﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.Logging;

namespace StyleMVVM.DependencyInjection.Conditions
{
	public class WhenAppSettingPresent : IExportCondition
	{
		private string settingName;
		private bool cacheAnswer;
		private bool? answer;

		public WhenAppSettingPresent(string settingName, bool cacheAnswer)
		{
			this.settingName = settingName;
			this.cacheAnswer = cacheAnswer;
		}

		public bool ConditionMeet(IDependencyInjectionContainer container,
		                          IInjectionContext injectionContext,
		                          IExportStrategy exportStrategy)
		{
			if (answer.HasValue)
			{
				return answer.Value;
			}

			bool returnValue = ConfigurationManager.AppSettings[settingName] != null;

			if (cacheAnswer)
			{
				answer = returnValue;
			}

			return returnValue;
		}
	}
}